<?php
/**
 * 图书章节内容管理控制器
 *
 * @author sandy
 *
 */
class chapterApp extends adminControl{

	/**
	 * 展示某一图书所有章节
	 */
	public function index(){
		$book_id = intval($_GET['bid']);
		// 获取当前页码及分页配置
		$page_info = get_page(20);
		
		$book_mod = &get_model('book');
		$chapter_mod = &get_model('chapter');
		
		$book_info = $book_mod->get_book_info($book_id);
		$chapters = $chapter_mod->get_book_chapter($book_id,$page_info);
	
		// 获取分页信息
		$page_info['total'] = $chapters['total'];
		$url = "index.php?c=chapter&bid=$book_id";
		$pages_data = format_page($page_info['total'], $page_info['cur_page'], $url, $page_info['per_page'],5);
		$pages = get_page_html($pages_data);
		
		$this->assign('pages',$pages);
		$this->assign('book_info', $book_info);
		$this->assign('chapters', $chapters['result']);
		$this->display();
	}
	
	/**
	 * 添加章节
	 */
	public function add(){
		$book_id = isset($_GET['bid']) ? intval($_GET['bid']): 0;
		$dosubmit = isset($_REQUEST['dosubmit']) ? $_REQUEST['dosubmit'] : false;

		
		if ($dosubmit) {	
			
			$chapter_info['chapter_title'] = isset($_POST['chapter_title']) ? trim($_POST['chapter_title']) : "";
			$chapter_info['book_id'] = isset ($_POST['book_id']) ? intval($_POST['book_id']) : "";
			$chapter_info['abstract'] = isset($_POST['abstract']) ?  trim($_POST['abstract']) : "";
			$chapter_info['sort'] = isset($_POST['sort']) ?  intval($_POST['sort']) : 0;
			
			$chapter_mod = &get_model('chapter');
			
			$return_url = $this->config->get_item('site_url').'/index.php?c=chapter&bid='.$book_id;
			if($chapter_mod->add_chapter($chapter_info)){
				$this->show_message( "添加章节成功!",$return_url);
			} else {
				$this->show_message("添加章节失败!",$return_url);
			}
			exit();
		}
				
		$book_mod = &get_model('book');
		$book_info = $book_mod->get_book_info($book_id);
		
		$this->assign('book_info',$book_info);
		$this->display();
	}
	
	/**
	 * 编辑章节信息
	 */
	public function edit(){
		$chapter_id = isset($_GET['cid']) ? intval($_GET['cid']) : 0;
		$book_id =  isset($_GET['bid']) ? intval($_GET['bid']): 0;
		$dosubmit = isset($_REQUEST['dosubmit']) ? $_REQUEST['dosubmit'] : false;
		
		$book_mod = &get_model('book');
		$chapter_mod = &get_model('chapter');

		if ($dosubmit) {	
			
			$chapter_info['chapter_id'] = $chapter_id;
			$chapter_info['chapter_title'] = isset($_POST['chapter_title']) ? trim($_POST['chapter_title']) : "";
			$chapter_info['book_id'] = isset ($_POST['book_id']) ? intval($_POST['book_id']) : "";
			$chapter_info['abstract'] = isset($_POST['abstract']) ?  trim($_POST['abstract']) : "";
			$chapter_info['sort'] = isset($_POST['sort']) ?  intval($_POST['sort']) : 0;
			
			$return_url = $this->config->get_item('site_url').'/index.php?c=chapter&a=edit&bid='.$book_id . "&cid=".$chapter_id;
			
			if($chapter_mod->update_chapter($chapter_info)){
				$this->show_message("编辑章节成功!",$return_url);
			} else {
				$this->show_message("编辑章节失败!",$return_url);
			}
			exit();
		}
		
		$book_info = $book_mod->get_book_info($book_id);
		$chapter_info =  $chapter_mod->get_chapter($chapter_id);
		
		$this->assign('book_info',$book_info);
		$this->assign('chapter_info', $chapter_info);
		$this->display();
	}
	
	/**
	 * 显示指定章节下所有文章信息 
	 */
	public function article(){
		$book_id = intval($_GET['bid']);
		$chapter_id = intval($_GET['cid']);
		
		// 获取当前页码及分页配置
		$page_info = get_page(20);
		$article_mod = &get_model('article');
		$book_mod = &get_model('book');
		$chapter_mod = &get_model('chapter');
		
		
		$book_info = $book_mod->get_book_info($book_id);
		$chapter_info =  $chapter_mod->get_chapter($chapter_id);
		$articles = $article_mod->get_chapter_article($chapter_id,$page_info);
		
		// 获取分页信息
		$page_info['total'] = $articles['total'];
		$url = "index.php?c=book";
		$pages_data = format_page($page_info['total'], $page_info['cur_page'], $url, $page_info['per_page'],5);
		$pages = get_page_html($pages_data);
		
		$this->assign('articles',$articles['result']);
		$this->assign('pages',$pages);
		$this->assign('book_info', $book_info);
		$this->assign('chapter_info', $chapter_info);
		
		$this->display();
	}
	
}